package com.reviewcode.cachemicroservice.controller;

import com.reviewcode.cachemicroservice.common.GenericResponse;
import com.reviewcode.cachemicroservice.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author vcorazon
 * @type Class
 * @description TODO
 * @since 2021/07/22 21:52
 */
@RestController
@RequestMapping("cache")
public class CacheApiController {

    @Autowired
    CacheService cacheService;

    @GetMapping("get")
    public GenericResponse getCache(@RequestParam String key){
        if(Objects.isNull(key)) return GenericResponse.success();
        return cacheService.getCache(key);
    }

    @PutMapping("store")
    public GenericResponse storeCache(@RequestParam String key,
                                      @RequestParam String value,
                                      @RequestParam(defaultValue = "10") String durationSeconds){
        if(Objects.isNull(key)) return GenericResponse.success();
        return cacheService.storeCache(key,value, Integer.parseInt(durationSeconds) * 1000);
    }
}
